package com.forever7776.life.admin.controller.life.m;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.admin.controller.BaseController;
import com.forever7776.life.core.entity.sys.SysRole;
import com.forever7776.life.core.service.sys.ISysRoleService;
import com.forever7776.life.remote.common.vo.ResultVO;
import enums.ResultEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import util.ResultUtil;

/**
 * 角色管理
 *
 * @author kz
 * @date 2018-01-15
 */
@Controller
@RequestMapping("/m/role")
public class ManageRoleController extends BaseController {

    @Autowired
    private ISysRoleService sysRoleService;


    @RequestMapping
    public String index() {
        return LIFE_ADMIN_VIEW_PAGE_PATH + "role/index";
    }

    /**
     * 查询角色列表
     *
     * @param roleName
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(@RequestParam(value = "roleName", required = false) String roleName) {
        Page<SysRole> page = sysRoleService.selectAll(new Page<SysRole>(pageNumber, pageSize), roleName);
        JSONObject result = new JSONObject();
        result.put("page", page);
        return result;
    }


    @RequestMapping("/{id}")
    @ResponseBody
    public JSONObject getById(@PathVariable String id) {
        SysRole role = sysRoleService.selectById(id);
        JSONObject result = ResultUtil.getSuccessResult("");
        result.put("role", role);
        return result;
    }


    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addRole(SysRole role) {
        JSONObject result = checkForm(role, "添加成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        sysRoleService.insert(role);
        return result;
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateRole(@PathVariable String id, SysRole role) {
        if (sysRoleService.selectById(id) == null) {
            return ResultUtil.getFailResult("该角色不存在！");
        }
        JSONObject result = checkForm(role, "编辑成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        sysRoleService.updateById(role);
        return result;
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO deleteRole(@PathVariable String id) {
        return sysRoleService.delete(id);
    }


    /**
     * 冻结角色
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/frozen/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject frozenRole(@PathVariable String id, @RequestParam(value = "status", defaultValue = "0") Integer status) {
        sysRoleService.frozen(id, status);
        return ResultUtil.getSuccessResult("操作成功！");
    }


    public JSONObject checkForm(SysRole role, String msg) {
        if (role == null) {
            return ResultUtil.getFailResult("角色为空");
        }
        if (StringUtils.isEmpty(role.getRoleName())) {
            return ResultUtil.getFailResult("角色名称为空");
        }
        if (StringUtils.isEmpty(role.getRoleCode())) {
            return ResultUtil.getFailResult("角色编码为空");
        }
        return ResultUtil.getSuccessResult(msg);
    }

}
